import { createReducer, Action, on } from '@ngrx/store';
import { setModalVisible, setModalType, setUserId, setLikedId, setShareInfo } from '@store/actions/member.actions';

export enum ModalTypes {
  Register = 'register',
  LoginByPhone = 'loginByPhone',
  Share = 'share',
  Like = 'like',
  Default = 'default'
}

export interface ShareInfo {
  id: string;
  type: string;
  txt: string;
}

export interface MemberState {
  modalVisible: boolean;
  modalType: ModalTypes;
  userId: string;
  likedId: string;
  shareInfo?: ShareInfo;
}

export const initialState: MemberState = {
  modalVisible: false,
  modalType: ModalTypes.Default,
  userId: '',
  likedId: ''
};

const reducer = createReducer(
  initialState,
  on(setModalVisible, (state, { modalVisible }) => ({...state, modalVisible})),
  on(setModalType, (state, { modalType }) => ({...state, modalType})),
  on(setUserId, (state, { userId }) => ({...state, userId})),
  on(setLikedId, (state, { likedId }) => ({...state, likedId})),
  on(setShareInfo, (state, { shareInfo }) => ({...state, shareInfo}))
);

export function memberReducer(state: MemberState, action: Action) {
  return reducer(state, action);
}
